package com.hrt.datasender.datadao.service;

import java.util.Calendar;
import java.util.Date;

import com.btcode.common.MyLog;
import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.log.ILog;
import com.hrt.db.DBManager;

public final class BdS1HourService {
    private static ILog log;
    private static volatile BdS1HourService instance;
    private final int dataRetentTime = 180;

    private BdS1HourService() {
        if (log == null) {
            log = MyLog.getInstance().getLogger(getClass());
        }
    }

    public static BdS1HourService getInstance() {
        if (instance == null) {
            synchronized (BdS1HourService.class) {
                if (instance == null) {
                    instance = new BdS1HourService();
                }
            }
        }
        return instance;
    }

    /**
     * 
     * <pre>
     * 定时清理sqlite小时数据库数据;数据保留180天
     * </pre>
     * 
     * @author FAN
     * @date 2018年9月17日下午3:29:26
     */
    public void clearRecord() {
        IDataExecuter dataExecuter = DBManager.getDataExecuter();
        String sql = " DELETE FROM BD_S1_HOUR WHERE DATATIME < @date  ";
        SQLParameterList pc = new SQLParameterList();
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -dataRetentTime);
        Date date = cal.getTime();
        pc.put("date", date);
        dataExecuter.delete(sql, pc);
    }

    /**
     * 
     * <pre>
     * 压缩sqlite数据库
     * </pre>
     * 
     * @author FAN
     * @date 2018年9月17日下午3:29:01
     */
    public void compressDataBase() {
        String sql = "vacuum";
        DBManager.getDataExecuter().queryList(sql, null);
    }

}